package huxibianjie.com.lbscandroid.ui.new_activity;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.Map;

import huxibianjie.com.lbscandroid.Constants;
import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.event.ClickEvent;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.widget.DownLoadImageService;
import huxibianjie.com.lbscandroid.widget.NoDoubleClickUtils;

/**
 * 进入微信社区 页面
 */
public class TaskCommunityActivity extends BaseActivity implements View.OnClickListener {
    private EditText etInput;
    private Button btnConfirm;
    private ImageView ivImage;

    @Override
    protected int getContentView() {
        return R.layout.activity_task_community;
    }


    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "进入社区");
        etInput = findViewById(R.id.et_input);
        btnConfirm = findViewById(R.id.btn_confirm);
        ivImage = findViewById(R.id.iv_image);

    }

    @Override
    protected void initData() {
        Glide.with(TaskCommunityActivity.this).load(Constants.ASSOCIATION).into(ivImage);
    }

    @Override
    protected void initListener() {
        btnConfirm.setOnClickListener(this);
        ivImage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (NoDoubleClickUtils.isDoubleClick()) return false;
                new android.support.v7.app.AlertDialog.Builder(TaskCommunityActivity.this).setTitle("提示").setMessage("确定保存图片？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                DownLoadImageService service = new DownLoadImageService(TaskCommunityActivity.this, Constants.ASSOCIATION, new DownLoadImageService.ImageDownLoadCallBack() {
                                    @Override
                                    public void onDownLoadSuccess(File file) {
                                    }

                                    @Override
                                    public void onDownLoadSuccess(Bitmap bitmap) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                showToast("保存图片成功");
                                            }
                                        });
                                    }

                                    @Override
                                    public void onDownLoadFailed() {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                showToast("保存图片失败");
                                            }
                                        });
                                    }
                                });
                                new Thread(service).start();
                            }
                        }).setNegativeButton("取消", null).show();
                return false;
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                String ids = String.valueOf(UserManager.getInstance().isLogin() ? UserManager.getInstance().getUserModel().getId() : "0");
                String code = etInput.getText().toString().trim();
                if (code.length() != 6) {
                    showToast("请输入正确的验证码");
                    return;
                }
                showLoading();
                Task.create(Service.class).communityTask(ids, code).enqueue(new Callback<Map<String, String>>() {
                    @Override
                    public void onSuccess(@Nullable Map<String, String> stringStringMap) {
                        showToast("验证成功");
                        EventBus.getDefault().post(new ClickEvent(ClickEvent.Type.REFRESH_USER_INFO, null, ""));
                        cancelLoading();
                        finish();
                    }

                    @Override
                    public void onFailure(@NonNull String error) {
                        showToast(error);
                        cancelLoading();
                    }
                });
                break;
        }
    }
}